/*
 * InputHandler - convert Flash events into per-frame button presses
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package flash3f;

#if flash9
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.Lib;
#end

class InputHandler
// this is a static class because input is a global attribute.
{
	public static var click : Bool;
	public static var wheeldelta : Bool;
	public static var rotleftkey : Bool;
	public static var rotrightkey : Bool;
	public static var cursx : Float = 250;
	public static var cursy : Float = 150;
	public static var rotleftkeydef : Int = "z".charCodeAt(0);
	public static var rotrightkeydef : Int = "x".charCodeAt(0);
	public static var pausekeydef : Int = "p".charCodeAt(0);
	public static var bqueue : Array<Bool> = new Array();
	public static var mqueue : Array<Float> = new Array();

	public static function start()
	{

		Lib.current.stage.addEventListener(flash.events.MouseEvent.CLICK, InputHandler.Click);
		Lib.current.stage.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, InputHandler.Move);	
		Lib.current.stage.addEventListener(flash.events.MouseEvent.MOUSE_WHEEL, InputHandler.Wheel);	
		Lib.current.stage.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, InputHandler.Keydown);

		bqueue.push(false);
		bqueue.push(false);
		bqueue.push(false);
		bqueue.push(false);
		mqueue.push(680/2);
		mqueue.push(425/2);
	}
	
	public static function clear()
	{
		click = bqueue[0];
		wheeldelta = bqueue[1];
		rotleftkey = bqueue[2];
		rotrightkey = bqueue[3];
		cursx = mqueue[0];
		cursy = mqueue[1];

		Recorder.up();

		bqueue[0] = false;
		bqueue[1] = false;
		bqueue[2] = false;
		bqueue[3] = false;
				
	}

#if flash9
	public static function Move(evt: MouseEvent)
	{
		mqueue[0] = evt.stageX;
		mqueue[1] = evt.stageY;
		
	}

	public static function Wheel(evt: MouseEvent)
	{

		bqueue[1] = true;
	
	}
	
	public static function Keydown(evt: KeyboardEvent)
	{
		
		if (evt.charCode==rotleftkeydef)
		{
			bqueue[2] = true;
		}
		if (evt.charCode==rotrightkeydef)
		{
			bqueue[3] = true;
		}		
	
	}
	
	public static function Click(evt: MouseEvent)
	{

		bqueue[0] = true;

		mqueue[0] = evt.stageX;
		mqueue[1] = evt.stageY;
		
	}
#end
}
